package com.haojiu.controller;

import com.haojiu.pojo.Notice;
import com.haojiu.pojo.User;
import com.haojiu.service.NoticeService;
import com.haojiu.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.List;

/**
 * @author:我叫浩九
 * @date：2021/8/28 10:37
 * create in RuGao JiangSu
 */
@Controller

public class UploadController {
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;
    @Autowired
    @Qualifier("NoticeServiceImpl")
    private NoticeService noticeService;

    @RequestMapping("/upload/add")
    public ModelAndView add(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        InputStream input = file.getInputStream();
        String fileName = file.getOriginalFilename();//获取上传文件的文件名
        System.out.println(fileName);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int len = -1;
        while ((len = input.read(b)) != -1) {
            bos.write(b, 0, len);
        }
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        User user = userService.queryUserByName(username);
        user.setAvator(bos.toByteArray());
        userService.updateUser(user);
        ModelAndView mv = new ModelAndView();
        List<Notice> noticeList = noticeService.queryAllNotice();
        mv.addObject("noticeList", noticeList);
        mv.setViewName("welcome");
        return mv;
    }

    @RequestMapping(value = "/showPicture")
    public void getPhotoById(HttpServletResponse response, HttpServletRequest request) throws IOException {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        User user = userService.queryUserByName(username);
        System.out.println(user);
        byte[] data = user.getAvator();
        response.setContentType("image/jpeg");
        response.setCharacterEncoding("UTF-8");
        OutputStream outputSream = response.getOutputStream();
        InputStream in = new ByteArrayInputStream(data);
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = in.read(buf)) != -1) {
            outputSream.write(buf, 0, len);
        }
        outputSream.flush();
        outputSream.close();
    }

}
